#include "Armor_blue.hpp"

int Armor_blue()
{
    // 创建一个VideoCapture对象
    VideoCapture cap("/home/hwx/project/video/armor_blue.avi");

    // 检查视频是否成功打开
    if (!cap.isOpened())
    {
        cerr << "Error: Could not open Video" << endl;
        return -1;
    }

    // 创建一个窗口用于显示视频
    namedWindow("Video", WINDOW_AUTOSIZE);

    // 循环读取视频每一帧
    while (true)
    {
        Mat frame, frameBW, frame_1, frameHSV;

        // 读取当前帧
        cap >> frame;

        // 检查帧是否为空
        if (frame.empty())
        {
            cerr << "Error: Could not read frame" << endl;
            break;
        }

        // 降噪处理
        //  高斯模糊
        GaussianBlur(frame, frame, Size(3, 3), 5, 0);
        // 均值模糊
        blur(frame, frame_1, Size(3, 3));

        // 将图片转化为HSV
        cvtColor(frame_1, frameHSV, COLOR_BGR2HSV);

        // 设置颜色阈值(蓝色)
        Scalar blue_lower(62, 82, 96);
        Scalar blue_upper(179, 255, 255);
        // 分割颜色,实现二值化
        inRange(frameHSV, blue_lower, blue_upper, frameBW);

        // //红色
        // Scalar red_lower(0, 69, 134);
        // Scalar red_upper(45, 255, 255);
        // // 分割颜色,实现二值化
        // inRange(frameHSV, red_lower, red_upper, frameBW);

        // 膨胀
        Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
        dilate(frameBW, frameBW, kernel, Point(-1, -1), 3);

        // 储存组成轮廓的点的集合
        vector<Point> contour;

        // 轮廓的集合
        vector<vector<Point>> contours;
        vector<Vec4i> hierarchy;

        // findContours()函数寻找轮廓
        findContours(frameBW, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

        // 储存灯条轮廓的索引
        vector<int> Lightcontours;

        // 灯条轮廓特点
        for (int i = 0; i < contours.size(); i++)
        {
            double area = contourArea(contours[i]);
            // boundingRect 获取最小正矩形
            Rect boundrect = boundingRect(contours[i]);
            // 计算面积比筛选噪点
            double Rectarea = boundrect.width * boundrect.height;
            double ratio = Rectarea * 1.0 / area * 1.0;

            // 获取外接矩形
            Rect rect = boundingRect(contours[i]);

            // 进一步筛选出灯条轮廓
            if (area > 950 && ratio > 0.8)
            {
                // 记录灯条的轮廓索引
                Lightcontours.push_back(i);
            }

            // 获取两两灯条的轮廓
            for (size_t i = 0; i < Lightcontours.size(); ++i)
            {
                for (size_t j = i + 1; j < Lightcontours.size(); ++j)
                {
                    int id_1 = Lightcontours[i];
                    int id_2 = Lightcontours[j];
                    if (!contours[id_1].empty() && !contours[id_2].empty())
                    {
                        // 装甲板轮廓集合
                        vector<Point> ArmorContour;

                        // 取两两灯条之间的轮廓作为装甲板的轮廓
                        ArmorContour.insert(ArmorContour.end(), contours[id_1].begin(), contours[id_1].end());
                        ArmorContour.insert(ArmorContour.end(), contours[id_2].begin(), contours[id_2].end());

                        // 装甲板最小外接矩形
                        Rect ArmorRect = boundingRect(ArmorContour);

                        // 定义装甲板颜色的变量和水平速度变量
                        string objName = "Color = blue ";

                        // 装甲板最小外接矩形可视化
                        if (ArmorRect.width < 300)
                        {
                            // 绘制最小外接矩形
                            rectangle(frame, ArmorRect, Scalar(255, 0, 0), 2);
                            putText(frame, objName, ArmorRect.tl(), FONT_HERSHEY_SIMPLEX, 1, Scalar(255, 0, 0), 2);
                        }
                    }
                }
            }
        }

        // 显示当前帧
        imshow("Video", frame);

        // 按下ESC键退出
        if (cv::waitKey(30) == 27)
        {
            break;
        }
    }

    // 释放视频
    cap.release();

    // 销毁窗口
    destroyAllWindows();
}
